package com.ddddddl.zhangben;

import java.util.ArrayList;
import java.util.Calendar;

import com.ddddddl.zhangben.data.Account;
import com.ddddddl.zhangben.logic.AccountLogic;
import com.ddddddl.zhangben.logic.ExecuteMessage;
import com.ddddddl.zhangben.logic.Request;
import com.ddddddl.zhangben.logic.RequestListener;

import static com.ddddddl.zhangben.logic.AccountLogic.*;
import com.ddddddl.zhangben.ui.adapter.AccountListAdapter;
import com.ddddddl.zhangben.util.CalendarUtils;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AccountListActivity extends BaseActivity implements OnClickListener, OnItemClickListener, RequestListener{
	
	public static final String PARAM_ACCOUNT = "PARAM_ACCOUNT";
	private static final int DIALOG_DELETE = 1;
	private ListView mAccountList;
	private AccountListAdapter mAccountListAdapter;
	private Button mDatePrev, mDateNext;
	private String mStartDate, mEndDate;
	private TextView mDateExtent, mAccountAmountSum;
	private Calendar mToday = Calendar.getInstance();
	private Account mSelectedAccount;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);
		
		mAccountList = (ListView) findViewById(R.id.account_list);
		mAccountListAdapter = new AccountListAdapter(this);
		mAccountList.setAdapter(mAccountListAdapter);
		mAccountList.setOnItemClickListener(this);
		
		mDatePrev = (Button) findViewById(R.id.date_prev);
		mDatePrev.setOnClickListener(this);
		
		mDateNext = (Button) findViewById(R.id.date_next);
		mDateNext.setOnClickListener(this);
		
		mDateExtent = (TextView) findViewById(R.id.date_extent);
		mAccountAmountSum = (TextView) findViewById(R.id.account_amount_sum);
		
		
		listen(AccountLogic.getInstance(), REQ_ACCOUNT_ADD, NOTIFY_AMOUNT_SUM_INIT, NOTIFY_AMOUNT_SUM_UPDATE, NOTIFY_TYPE_UPDATE);
		getAccountListByDate();
		
	}

	private void getAccountListByDate() {
		int maxDay = mToday.getMaximum(Calendar.DAY_OF_MONTH);
		mStartDate = CalendarUtils.format(mToday.get(Calendar.YEAR), mToday.get(Calendar.MONTH), 1).toString();
		mEndDate = CalendarUtils.format(mToday.get(Calendar.YEAR), mToday.get(Calendar.MONTH), maxDay).toString();
		
		mDateExtent.setText(mStartDate + "-" + mEndDate);
		
		Request request = new Request(REQ_ACCOUNT_LIST, this);
		request.putString(AccountLogic.PARAM_START_DATE, mStartDate);
		request.putString(AccountLogic.PARAM_END_DATE, mEndDate);
		AccountLogic.getInstance().sendRequest(request);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.edit_account:
			Intent intent = new Intent(this, EditActivity.class);
			intent.putExtra(PARAM_ACCOUNT, mAccountListAdapter.getSelectedItem());
			startActivity(intent);
			break;
		case R.id.delete_account:
			showDialog(DIALOG_DELETE);
			break;
		case R.id.date_prev:	
			mToday.add(Calendar.MONTH, -1);
			getAccountListByDate();
			break;
		case R.id.date_next:	
			mToday.add(Calendar.MONTH, 1);
			getAccountListByDate();
			break;
		default:
			break;
		}
	}
	
	protected void onPrepareDialog(int id, Dialog dialog) {
		switch (id) {
		case DIALOG_DELETE:
			mSelectedAccount = mAccountListAdapter.getSelectedItem();
			break;
		default:
			break;
		}
	}
	
	private Button ok, cancl;
	
	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog;
		LinearLayout view;
		switch (id) {
		case DIALOG_DELETE:
			dialog = new Dialog(this);
			dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
			view = (LinearLayout) View.inflate(this, R.layout.dialog, null);
			
			ok = (Button) view.findViewById(R.id.button_ok);
	        ok.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					AccountLogic.getInstance().deleteAccount(mSelectedAccount);
					
					mAccountListAdapter.deleteSelectedItem();
					mAccountListAdapter.selectedNothing();
					
					removeDialog(DIALOG_DELETE);
				}
			});
	        
	        cancl = (Button) view.findViewById(R.id.button_cancl);
	        cancl.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					removeDialog(DIALOG_DELETE);
				}
			});
	        dialog.setContentView(view);
			return dialog;
		default:
			return super.onCreateDialog(id);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		switch (parent.getId()) {
		case R.id.account_list:
			mAccountListAdapter.setSelected(position);
			mAccountListAdapter.notifyDataSetChanged();
			break;
		default:
			break;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public void onRequestFinished(Request request) {
		switch (request.type) {
		case REQ_ACCOUNT_LIST:
			mAccountListAdapter.setData((ArrayList<Account>) request.getResponseObject(PARAM_ACCOUNTS));
			break;
		default:
			break;
		}
		
	}

	@Override
	public void onNotify(ExecuteMessage executeMessage) {
		switch (executeMessage.type) {
		case REQ_ACCOUNT_ADD:
			mAccountListAdapter.addAccount((Account) executeMessage.get(PARAM_ACCOUNT));
			break;
		case NOTIFY_AMOUNT_SUM_INIT:
		case NOTIFY_AMOUNT_SUM_UPDATE:
			mAccountAmountSum.setText(executeMessage.getString(PARAM_AMOUNT_SUM));
			break;
		case NOTIFY_TYPE_UPDATE:
			mAccountListAdapter.notifyDataSetChanged();
		default:
			break;
		}
	}
	
}
